package loader;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;

import org.apache.log4j.Logger;

import common.NumberVO;

/**
 * The Class HistoryLoader.
 */
public class HistoryLoader {
    
    /** Delimiter. */
    static final String DELIMITER = "||";
    
    /** log4j logger */
    static final Logger logger = Logger.getLogger(HistoryLoader.class);

    /**
     * Load history text file.
     *
     * @return the hash map
     */
    static public HashMap<String, NumberVO> loadHistoryTextFile(String fileName, HashMap<String, NumberVO> data) throws Exception {
        if (data == null) {
            throw new IllegalArgumentException("Invalid Argument - data holder is null");
        }
        
        // Data File 접근
        File dataFile = null;
        
        URL fileURL = getFileURL(fileName);
        
        if (fileURL == null) {
            if ( validateAbsolutePath(fileName) ) {
                dataFile = new File(fileName);
            } else {
                throw new RuntimeException("Error cannot load data file");
            }
        } else {
            dataFile = new File(fileURL.toURI());
        }
        
        FileReader rd = null;
        BufferedReader buffer = null;
        
        int readCount = 0;
        
        // Data File parsing
        try {
            rd = new FileReader(dataFile);
            buffer = new BufferedReader(rd);
                        
            String readLine = null;
            
            do {
                readLine = buffer.readLine();
                
                if (readLine != null) {
                    readCount++;
                    
                    String[] numberTokens = readLine.split("\\|\\|");
                    
                    if (numberTokens.length > 0) {
                        String gameIndex = numberTokens[0];
                        
                        String number = null;
                        
                        for (int idx = 1 ; idx < numberTokens.length - 1 ; idx++) {
                            number = numberTokens[idx];
                            
                            NumberVO numberVO = null;
                            
                            if ( !data.containsKey(number) ) {
                                numberVO = new NumberVO(Integer.parseInt(number));
                            }
                            
                            numberVO.addWinning(Integer.parseInt(gameIndex));
                            
                            data.put(number, numberVO);
                        }
                        
                        // Bonus 볼은 일단 skip 한다.
                    }
                    
                } else break;
                
            } while (true);
            
        } catch (RuntimeException e) {
            
        } finally {
            buffer.close();
            rd.close();
        }
        
        return data;
    }
    
    static private URL getFileURL(String fileName) {
        URL resultURL = null;
        
        // Find the destination file in Classpath of this instance's container or VM
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        
        if (cl == null) {
            cl = ClassLoader.getSystemClassLoader();
        } else 
        
        resultURL = cl.getResource(fileName);
                
        return resultURL;
    }
    
    static private boolean validateAbsolutePath(String fileName) {
        File validation = new File(fileName);
        return validation.isFile();
    }
}
